# Maintainer: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=blosc2
_srcname=c-blosc2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.4
pkgrel=1
pkgdesc="A blocking, shuffling and lossless compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://blosc.org/'
license=('BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/Blosc/c-blosc2/archive/v${pkgver}.tar.gz")
sha256sums=('90c78edcc262759dd16d243141513310624bb4fda3d98ac34dcfb78255e151c1')

prepare() {
  cd "${srcdir}"/${_srcname}-${pkgver}
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DPREFER_EXTERNAL_LZ4=ON \
      -DPREFER_EXTERNAL_ZLIB=ON \
      -DPREFER_EXTERNAL_ZSTD=ON \
      -DBUILD_TESTS=OFF \
      -DBUILD_FUZZERS=OFF \
      -DBUILD_BENCHMARKS=OFF \
      -DBUILD_EXAMPLES=OFF \
      ../${_srcname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_srcname}-${pkgver}/LICENSE.txt "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
